#include "gd32f3x0.h"
#include "Log.h"

typedef void (*NewEntry)(void);
NewEntry JumpEntry;

void iap_load_app(uint32_t newappaddr)
{
    uint32_t firstdword = (*(__IO uint32_t*)newappaddr);
    LOG_PRINT(("Firstword: 0x%X\r\n", firstdword));
    if(((*(__IO uint32_t*)newappaddr) & 0x2FFE0000) == 0x20000000)
    {
        __disable_irq();
        JumpEntry = (NewEntry)*(__IO uint32_t*)(newappaddr + 4);
        __set_MSP(*(__IO uint32_t*) newappaddr);  
        LOG_PRINT(("Jump to New Entry...\r\n\n"));
        JumpEntry();
    }
    else
    {
        LOG_PRINT(("No Application Found!\r\n"));
    }
}
